#include "workManager.h"


//管理类负责的内容如下：
//1、与用户的沟通菜单界面
//2、对职工增删改查的操作
//3、与文件的读写交互
WorkManager::WorkManager() {//构造函数，初始化属性
    ifstream ifs;
    char ch;
    ifs.open(FILENAME, ios::in);
    if (!ifs.is_open()) {//文件不存在情况
        //cout << "文件不存在" << endl;
        this->EmpNum = 0;//初始化人数
        this->EmpArray = nullptr;//初始化数组指针
        ifs.close();//关闭文件
        return;
    }
    ifs >> ch;
    if (ifs.eof()) {
        //cout << "文件为空" << endl;
        this->EmpNum = 0;//初始化人数
        this->FileIsEmpty = true;//初始化文件为空标志
        this->EmpArray = nullptr;//初始化数组指针
        ifs.close();//关闭文件
        return;
    }
    int num = getEmpNum();//获取记录的职工人数
    //cout << "职工个数为：" << num << endl;//测试代码
    this->EmpNum = num;//更新成员属性
    this->EmpArray = new Worker *[this->EmpNum];//开辟空间
    this->initEmp();//将文件中的数据存放到数组中
//    for (int i = 0; i < this->EmpNum; ++i) {
//        cout<<"职工编号："<< this->EmpArray[i]->id
//        <<",姓名："<<this->EmpArray[i]->name
//        <<",部门编号："<< this->EmpArray[i]->deptId
//        <<endl;
//    }
}

void WorkManager::showMenu() {//菜单功能：与用户的沟通界面
    cout << "====================================" << endl;
    cout << "======  欢迎使用职工管理系统！ ======" << endl;
    cout << "=========  0.退出管理程序  =========" << endl;
    cout << "=========  1.增加职工信息  =========" << endl;
    cout << "=========  2.显示职工信息  =========" << endl;
    cout << "=========  3.删除离职职工  =========" << endl;
    cout << "=========  4.修改职工信息  =========" << endl;
    cout << "=========  5.查找职工信息  =========" << endl;
    cout << "=========  6.按照编号排序  =========" << endl;
    cout << "=========  7.清空所有文档  =========" << endl;
    cout << "====================================" << endl;
}

void WorkManager::save() {//保存文件
    ofstream ofs;
    ofs.open(FILENAME, ios::out);//用输出的方式打开文件--写文件
    for (int i = 0; i < this->EmpNum; ++i) {
        ofs << this->EmpArray[i]->id << " "
            << this->EmpArray[i]->name << " "
            << this->EmpArray[i]->deptId << endl;
    }
    ofs.close();
}

int WorkManager::getEmpNum() {//统计文件中的职工人数
    ifstream ifs;
    ifs.open(FILENAME, ios::in);

    int id;
    string name;
    int deptId;
    int num = 0;
    while (ifs >> id && ifs >> name && ifs >> deptId) {
        num++;
    }
    ifs.close();
    return num;
}

void WorkManager::initEmp() {//初始化员工
    ifstream ifs;
    ifs.open(FILENAME, ios::in);
    int id;
    string name;
    int deptId;
    int index = 0;
    while (ifs >> id && ifs >> name && ifs >> deptId) {
        Worker *worker = nullptr;
        //根据不同的部门Id创建不同对象
        if (deptId == 1) {// 1普通员工
            worker = new Employee(id, name, deptId);
        } else if (deptId == 2) { //2经理
            worker = new Manager(id, name, deptId);
        } else {//总裁
            worker = new Boss(id, name, deptId);
        }
        this->EmpArray[index] = worker;
        index++;
    }
    ifs.close();//关闭文件
}

void WorkManager::addEpm() {//添加职工
    int addNum;
    cout << "请输入添加职工的数量:";
    cin >> addNum;
    if (addNum > 0) {//添加
        int newSize = this->EmpNum + addNum;//计算添加新空间的大小,新空间的人数=原来记录的人数+新增人数
        Worker **newSpace = new Worker *[newSize];//开辟新空间
        if (this->EmpArray != nullptr) {//将原来空间下的数据，拷贝到新空间下
            for (int i = 0; i < this->EmpNum; i++) {
                newSpace[i] = this->EmpArray[i];
            }
        }
        for (int i = 0; i < addNum; i++) {//批量添加新数据
            int id;//职工编号
            string name;//职工姓名
            int dSelect;//部门选择
            cout << "请输入第 " << i + 1 << " 个新职工编号：";
            cin >> id;
            cout << "请输入第 " << i + 1 << " 个新职工姓名：";
            cin >> name;
            cout << "请选择该职工的岗位：" << endl;
            cout << "1、普通职工" << endl;
            cout << "2、经理" << endl;
            cout << "3、老板" << endl;
            cin >> dSelect;
            Worker *worker = nullptr;
            switch (dSelect) {
                case 1://普通员工
                    worker = new Employee(id, name, 1);
                    break;
                case 2:
                    worker = new Manager(id, name, 2);
                    break;
                case 3:
                    worker = new Boss(id, name, 3);
                    break;
                default:
                    break;;
            }
            newSpace[this->EmpNum + i] = worker;//将创建的职工，保存到数组中
        }
        delete[] this->EmpArray;//释放原有的空间
        this->EmpArray = newSpace;//更改新空间的指向
        this->EmpNum = newSize;//更新新的职工人数
        this->FileIsEmpty = false;//更新职工不为空标志
        cout << "成功添加" << addNum << "名新职工！" << endl;//提示信息
        this->save();//成功添加后，保存数据到文件中
    } else {
        cout << "输入的数据非法!" << endl;
    }
}

void WorkManager::showEpm() {//显示职工
    if (this->FileIsEmpty) {
        cout << "文件不存在或记录为空！" << endl;
    } else {
        for (int i = 0; i < this->EmpNum; ++i) {
            this->EmpArray[i]->showInfo();//利用多态调用接口
        }
    }
}

int WorkManager::IsExist(int id) {//职工是否存在
    int index = -1;
    for (int i = 0; i < this->EmpNum; ++i) {
        if (this->EmpArray[i]->id == id) {
            index = i;
        }
    }
    return index;
}

void WorkManager::delEmp() {//删除职工
    if (this->FileIsEmpty) {
        cout << "文件不存在或记录为空！" << endl;
    } else {
        //按职工编号删除
        cout << "请输入想要删除的职工号：";
        int id = 0;
        cin >> id;

        int index = this->IsExist(id);
        if (index != -1) {//说明index上位置数据需要删除
            for (int i = index; i < this->EmpNum; ++i) {
                this->EmpArray[i] = this->EmpArray[i + 1];
            }
            this->EmpNum--;
            this->save();//删除后数据同步到文件中
            cout << "删除成功" << endl;
        } else {
            cout << "删除失败，未找到该职工" << endl;
        }
    }
}

void WorkManager::modEmp() {//修改职工
    if (this->FileIsEmpty) {
        cout << "文件不存在或记录为空！" << endl;
    } else {
        cout << "请输入修改职工的编号：" << endl;
        int id;
        cin >> id;

        int ret = this->IsExist(id);
        if (ret != -1) {
            delete this->EmpArray[ret];

            int newId = 0;
            string newName = "";
            int dSelect = 0;

            cout << "查到：" << id << "号职工，请输入新的职工号：";
            cin >> newId;
            cout << "请输入新的姓名：";
            cin >> newName;
            cout << "请输入岗位： " << endl;
            cout << "1、普通职工" << endl;
            cout << "2、经理" << endl;
            cout << "3、老板" << endl;
            cin >> dSelect;

            Worker *worker = nullptr;
            switch (dSelect) {
                case 1:
                    worker = new Employee(newId, newName, dSelect);
                    break;
                case 2:
                    worker = new Manager(newId, newName, dSelect);
                    break;
                case 3:
                    worker = new Boss(newId, newName, dSelect);
                    break;
                default:
                    break;
            }
            this->EmpArray[ret] = worker;//更改数据 到数组中
            cout << "修改成功" << endl;
            this->save();//保存到文件中
        } else {
            cout << "修改失败，查无此人" << endl;
        }
    }
}

void WorkManager::findEmp() {//查找职工
    if (this->FileIsEmpty) {
        cout << "文件不存在或记录为空！" << endl;
    } else {
        cout << "请输入查找的方式：" << endl;
        cout << "1、按职工编号查找" << endl;
        cout << "2、按姓名查找" << endl;
        int select;
        cout << "请输入查找的方式:";
        cin >> select;
        if (select == 1) //按职工号查找
        {
            int id;
            cout << "请输入查找的职工编号：";
            cin >> id;

            int ret = IsExist(id);
            if (ret != -1) {
                cout << "查找成功！该职工信息如下：" << endl;
                this->EmpArray[ret]->showInfo();
            } else {
                cout << "查找失败，查无此人" << endl;
            }
        } else if (select == 2) {//按姓名查找
            string name;
            cout << "请输入查找的姓名:";
            cin >> name;

            bool flag = false;//查找到的标志
            for (int i = 0; i < EmpNum; ++i) {
                if (EmpArray[i]->name == name) {
                    cout << "查找成功,职工编号为：" << EmpArray[i]->id << " 号的信息如下：" << endl;
                    flag = true;
                    this->EmpArray[i]->showInfo();
                }
            }
            if (!flag) {
                cout << "查找失败，查无此人" << endl;
            }
        } else {
            cout << "输入的数据有误" << endl;
        }
    }
}

void WorkManager::sortEmp() {//排序
    if (this->FileIsEmpty) {
        cout << "文件不存在或记录为空！" << endl;
    } else {
        cout << "请选择排序方式： " << endl;
        cout << "1、按职工号进行升序" << endl;
        cout << "2、按职工号进行降序" << endl;
        int select;
        cout << "请输入排序方式:";
        cin >> select;
        for (int i = 0; i < EmpNum; ++i) {
            int MinOrMax = i;
            for (int j = i + 1; j < EmpNum; ++j) {
                if (select == 1) {
                    if (EmpArray[MinOrMax]->id > EmpArray[j]->id) {
                        MinOrMax = j;
                    }
                } else {
                    if (EmpArray[MinOrMax]->id < EmpArray[j]->id) {
                        MinOrMax = j;
                    }
                }
            }
            if (i != MinOrMax) {
                Worker *temp = EmpArray[i];
                EmpArray[i] = EmpArray[MinOrMax];
                EmpArray[MinOrMax] = temp;
            }
        }
        cout << "排序成功，排序后的结果为:" << endl;
        this->save();
        this->showEpm();
    }
}

void WorkManager::cleanFile() {//清空文件
    cout << "确认清空？" << endl;
    cout << "1、确认" << endl;
    cout << "2、返回" << endl;
    int select;
    cout << "请输入你的选择:";
    cin >> select;
    if (select == 1) {
        ofstream ofs(FILENAME, ios::trunc);
        ofs.close();
        if (this->EmpArray != nullptr) {
            for (int i = 0; i < this->EmpNum; ++i) {
                if (this->EmpArray[i] != nullptr) {
                    delete this->EmpArray[i];
                }
            }
            this->EmpNum = 0;
            delete[] this->EmpArray;
            this->EmpArray = nullptr;
            this->FileIsEmpty = true;
        }
        cout << "清空成功" << endl;
    }
}

WorkManager::~WorkManager() {//析构函数
    if (this->EmpArray != nullptr) {
        delete[] this->EmpArray;
        this->EmpArray = nullptr;
    }
}















